/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"

extern const Ipp8u _nIZigZagIndex[64];

/******************************************************************************
// Name:            ijHuffmanEncode_Gray_BLS
// Description:     Huffman encoder (JPEG Baseline) for 1 component image.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanEncode_Gray_BLS(const Ipp16s **ppMCUBuf, 
                                   IJBITTREAMSTATE *pStream,
                                   IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[0],
                                         pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         &(pEncodeState->dcPred[0]),
                                         pEncodeState->paIdxDCHuffTable[0],
                                         pEncodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanEncode_444_BLS
// Description:     Huffman encoder (JPEG Baseline) for 3 components image in 444 resampling.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanEncode_444_BLS(const Ipp16s **ppMCUBuf, 
                           IJBITTREAMSTATE *pStream,
                           IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[0],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[0]),
                                   pEncodeState->paIdxDCHuffTable[0],
                                   pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[1],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[1]),
                                   pEncodeState->paIdxDCHuffTable[1],
                                   pEncodeState->paIdxACHuffTable[1]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[2],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[2]),
                                   pEncodeState->paIdxDCHuffTable[2],
                                   pEncodeState->paIdxACHuffTable[2]);

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanEncode_422_BLS
// Description:     Huffman encoder (JPEG Baseline) for 3 components image in 422 resampling.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanEncode_422_BLS(const Ipp16s **ppMCUBuf, 
                           IJBITTREAMSTATE *pStream,
                           IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[0],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[0]),
                                   pEncodeState->paIdxDCHuffTable[0],
                                   pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( &(ppMCUBuf[0][64]),
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[0]),
                                   pEncodeState->paIdxDCHuffTable[0],
                                   pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[1],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[1]),
                                   pEncodeState->paIdxDCHuffTable[1],
                                   pEncodeState->paIdxACHuffTable[1]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[2],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[2]),
                                   pEncodeState->paIdxDCHuffTable[2],
                                   pEncodeState->paIdxACHuffTable[2]);

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanEncode_411_BLS
// Description:     Huffman encoder (JPEG Baseline) for 3 components image in 411 resampling.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanEncode_411_BLS(const Ipp16s **ppMCUBuf, 
                           IJBITTREAMSTATE *pStream,
                           IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[0],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[0]),
                                   pEncodeState->paIdxDCHuffTable[0],
                                   pEncodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( &(ppMCUBuf[0][64]),
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[0]),
                                   pEncodeState->paIdxDCHuffTable[0],
                                   pEncodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( &(ppMCUBuf[0][128]),
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[0]),
                                   pEncodeState->paIdxDCHuffTable[0],
                                   pEncodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( &(ppMCUBuf[0][192]),
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[0]),
                                   pEncodeState->paIdxDCHuffTable[0],
                                   pEncodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[1],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[1]),
                                   pEncodeState->paIdxDCHuffTable[1],
                                   pEncodeState->paIdxACHuffTable[1]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u ( ppMCUBuf[2],
                                   pStream->pBitStream,  
                                   &(pStream->nUsedBufBitsLen),
                                   &(pEncodeState->dcPred[2]),
                                   pEncodeState->paIdxDCHuffTable[2],
                                   pEncodeState->paIdxACHuffTable[2]);

//    pEncodeState->counter.nScanMCUCounter++;

    if ( (0<pEncodeState->nRstInterval) ){
        pEncodeState->counter.nDRICounter--;
        if (0==pEncodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalFwd(pStream,pEncodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/* EOF */
